//
//  MeSctionOneController.m
//  FWeChat
//
//  Created by iSolar on 2017/12/6.
//  Copyright © 2017年 NOOrganization. All rights reserved.
//

#import "MeSctionOneController.h"
#import "FWeChat-Swift.h"

@interface MeSctionOneController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;


@end

@implementation MeSctionOneController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.tableView.superview);
        make.bottom.equalTo(@(KBottomMargin));
    }];
    _dataArray = @[@[@{@"img":@"robot",@"title":@"头像"},@{@"title":@"名字",@"subtitle":@"小宇宙"},@{@"subtitle":@"走不走",@"title":@"微信号"},@{@"erImg":@"er",@"title":@"我的二维码"},@{@"title":@"更多",@"subtitle":@""}],@[@{@"title":@"我的地址",@"subtitle":@""}]];
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // tableView 偏移20/64适配
        if (@available(iOS 11.0, *)) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [_tableView registerNib:[UINib nibWithNibName:@"MeIconViewCell" bundle:nil] forCellReuseIdentifier:@"cell0"];
        [_tableView registerNib:[UINib nibWithNibName:@"MeDescViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView.separatorColor = [UIColor colorWithHexString:@"e2e2e2"];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"eeecf4"];
    }
    
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArray = _dataArray[section];
    
    return sectionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *sectionArray = _dataArray[indexPath.section];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        MeIconViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell0"];
        if (!cell) {
            cell = [[MeIconViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell0"];
        }
        cell.iconView.image = [UIImage imageNamed:sectionArray[indexPath.row][@"img"]];
        cell.titleLabel.text = sectionArray[indexPath.row][@"title"];
        
        return cell;
    }
    
    static NSString *cellIndentifier = @"cell";
    MeDescViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (cell == nil) {
        cell = [[MeDescViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.erImgV.hidden = YES;
    if (indexPath.section == 0 && indexPath.row == 3) {
        cell.erImgV.image = [UIImage imageNamed:sectionArray[indexPath.row][@"img"]];
    }
    cell.titleLabel.text = sectionArray[indexPath.row][@"title"];
    cell.subTitleLabel.text = sectionArray[indexPath.row][@"subtitle"];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 100;
    }
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSArray *sectionArray = _dataArray[indexPath.section];
//    if (indexPath.section == 0) {
//
//    }
//
}

// tableView 如果是Gruop类型的话，section之间的间距变宽，执行返回高度的同时还需要执行return UIView的代理
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

@end
